/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef FOUNDATION_ACE_ADAPTER_OHOS_KEELS_ENTRANCE_WINDOW_VIEW_H
#define FOUNDATION_ACE_ADAPTER_OHOS_KEELS_ENTRANCE_WINDOW_VIEW_H

#include <ace/xcomponent/native_interface_xcomponent.h>
#include <arkui/native_interface.h>
#include <arkui/native_node.h>

#include "base/utils/macros.h"
#include "virtual_rs_window.h"

namespace Keels::Ace::Platform {
class WindowView;
class ACE_EXPORT WindowViewManager {
public:
    static ArkUI_NativeNodeAPI_1* GetNodeApi();
    static OH_NativeXComponent_Callback* GetXComponentCallback();
    static uint32_t GetNewWindowId();
    static std::string GetXComponentId(OH_NativeXComponent* component);
    static std::shared_ptr<Rosen::Window> GetWindow(OH_NativeXComponent* component);
    static void RegisterWindowView(OH_NativeXComponent* component, WindowView* windowView);
    static void UnregisterWindowView(OH_NativeXComponent* component);

private:
    struct XcInfo {
        OH_NativeXComponent* component = nullptr;
        void* window = nullptr;

        XcInfo(OH_NativeXComponent* xc, void* w) : component(xc), window(w) {}
    };

    WindowViewManager();
    ~WindowViewManager() = default;

    static WindowViewManager& GetInstance();

    static void OnSurfaceCreated(OH_NativeXComponent* component, void* window);
    static void OnSurfaceChanged(OH_NativeXComponent* component, void* window);
    static void OnSurfaceDestroyed(OH_NativeXComponent* component, void* window);
    static void DispatchTouchEvent(OH_NativeXComponent* component, void* window);
    static bool BeforeSurfaceCreated(OH_NativeXComponent* component, void* window);
    static void AfterSurfaceDestroyed(OH_NativeXComponent* component, void* window);

    bool isInitialized_ = false;
    uint32_t windowCnt_ = 0;
    ArkUI_NativeNodeAPI_1* api_ = nullptr;
    OH_NativeXComponent_Callback callback_ {};
    std::list<XcInfo> xComponentList_ {};
    std::unordered_map<OH_NativeXComponent*, WindowView*> componentToWindowViewMap_ {};
};

class ACE_EXPORT WindowView {
public:
    WindowView();
    ~WindowView();

    ArkUI_NodeHandle GetNodeHandle() const;
    void DisposeNodeHandle();
    bool IsReady() const;
    bool IsDestroyed() const;
    void UpdateLayout(float width, float height, float left, float top);
    void MountThirdPartyUIRoot(const void* uiRoot);
    void UnmountThirdPartyUIRoot();

private:
    static void NodeEventReceiver(ArkUI_NodeEvent*);

    friend class WindowViewManager;
    void Initialize();

    bool isReady_ = false;
    bool isDestroyed_ = false;
    ArkUI_NodeHandle xComponent_ = nullptr;
    OH_NativeXComponent* component_ = nullptr;
    std::weak_ptr<Rosen::Window> rsWindow_ {};
};
} // namespace Keels::Ace::Platform

#endif // FOUNDATION_ACE_ADAPTER_OHOS_KEELS_ENTRANCE_WINDOW_VIEW_H